<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "ROOTPATH", "../" );
include( ROOTPATH."includes/admin.inc.php" );
include( ROOTPATH."base/admin/language/".$sLan.".php" );
include( ROOTPATH."base/nusoap/nusoap.php" );
include( ROOTPATH."includes/data.inc.php" );
$server = "http://update.phpweb.net/remote/webservice/soapserver.php";
$customer = new soapclientx( $server );
needauth( 7 );
$act = $_POST['act'];
switch ( $act )
{
case "getUpdateList" :
				$r_params = array(
								"siteurl" => $SiteUrl,
								"domain" => $_SERVER['HTTP_HOST']
				);
				$lic = $customer->call( "getUpdateInfo", $r_params );
				if ( $err = $customer->geterror( ) )
				{
								$errinfo = $customer->response;
								echo "<div class='update_err'>升级服务器连接失败<br>".$err."<br>".$errinfo."</div>";
								exit( );
				}
				if ( !$lic )
				{
								echo "<div class='update_err'>升级服务器连接失败，请稍候再试</div>";
								exit( );
				}
				$nums = sizeof( $lic );
				$n = 0;
				$caninstall = "1";
				$i = 0;
				for ( ;	$i < $nums;	$i++	)
				{
								$coltype = $lic[$i]['coltype'];
								$title = $lic[$i]['title'];
								$release = $lic[$i]['release'];
								$version = $lic[$i]['version'];
								$msg = $lic[$i]['msg'];
								$msql->query( "select colname from {P}_base_coltype where coltype='{$coltype}'" );
								if ( $msql->next_record( ) )
								{
												$colname = "[".$msql->f( "colname" )."]";
												$ColCanUpdate = "1";
								}
								else
								{
												$colname = "";
												$ColCanUpdate = "0";
								}
								if ( $ColCanUpdate == "1" || $coltype == "all" )
								{
												if ( intval( $release ) <= intval( PHPWEB_RELEASE ) )
												{
																$msql->query( "select `release` from {P}_base_version where `release`='{$release}'" );
																if ( $msql->next_record( ) )
																{
																				$str .= "<div id='uo_".$i."' class='update_ok'><div class='u_open'></div>".$release."/".$version." &nbsp; ".$colname." &nbsp;".$title." </div>";
																}
																else
																{
																				$str .= "<div id='uo_".$i."' class='update_ok' style='background:#f7f7f7 url(images/alert.png) 15px no-repeat'><div class='u_open'></div>".$release."/".$version." &nbsp; ".$colname." &nbsp;".$title." (升级未安装，因升级时模块未安装或数据更新出错)</div>";
																				$caninstall = "1";
																}
																$str .= "<div id=suo_".$i." class='update_detail'>".nl2br( $msg )."</div>";
												}
												else
												{
																if ( file_exists( "../update/".$release."/version.php" ) )
																{
																				if ( $n == 0 )
																				{
																								$bstr = "<div class='u_right'><a href='#' id='inst_".$release."' class='u_inst'>安装升级</a></div>";
																				}
																				else
																				{
																								$bstr = "<div class='u_right'>升级已上传</div>";
																				}
																}
																else
																{
																				$bstr = "<div class='u_right'>升级未上传</div>";
																}
																$msql->query( "select `release` from {P}_base_version where `release`='{$release}'" );
																if ( $msql->next_record( ) )
																{
																				$str .= "<div id='uo_".$i."' class='update_no' style='background:#f7f7f7 url(images/alert.png) 15px no-repeat'><div class='u_open'></div>".$release."/".$version." &nbsp; ".$colname." &nbsp;".$title." &nbsp; (数据已更新但文件系统升级不完整)</div>";
																				$caninstall = "0";
																}
																else
																{
																				$str .= "<div id='uo_".$i."' class='update_no'><div class='u_open'></div>".$release."/".$version." &nbsp; ".$colname." &nbsp;".$title." </div>";
																}
																$str .= "<div id=suo_".$i." class='update_detail'>".$bstr.nl2br( $msg )."</div>";
																$n++;
												}
								}
				}
				$str .= "<input id='caninstall' type='hidden' value='".$caninstall."'><input id='phpwebUser' type='hidden' value='".$GLOBALS['CONF']['phpwebUser']."'>";
				echo $str;
				exit( );
				break;
case "installUpdate" :
				$release = $_POST['r'];
				$user = $_POST['user'];
				$passwd = $_POST['passwd'];
				$r_params = array(
								"siteurl" => $SiteUrl,
								"domain" => $_SERVER['HTTP_HOST'],
								"release" => $release,
								"user" => $user,
								"passwd" => $passwd
				);
				$lic = $customer->call( "installUpdate", $r_params );
				if ( $err = $customer->geterror( ) )
				{
								$errinfo = $customer->response;
								echo "<div class='update_err'>升级服务器连接失败<br>".$err."<br>".$errinfo."</div>";
								exit( );
				}
				if ( !$lic )
				{
								echo "<div class='update_err'>升级服务器连接失败，请稍候再试</div>";
								exit( );
				}
				else if ( $lic == "nouser" )
				{
								echo "<div class='update_err'>授权用户名或密码错误，软件升级服务验证未通过</div>";
								exit( );
				}
				else if ( $lic == "norights" )
				{
								echo "<div class='update_err'>软件升级服务验证未通过，可能是因为软件尚未购买商业授权</div>";
								exit( );
				}
				else if ( $lic == "exp" )
				{
								echo "<div class='update_err'>软件尚未购买商业授权或已过期，请续订商业授权</div>";
								exit( );
				}
				if ( strstr( $lic, ";" ) )
				{
								$lic = remove_remarks( trim( $lic ) );
								$pieces = split_sql_file( $lic, ";" );
								$pieces_count = count( $pieces );
								$i = 0;
								for ( ;	$i < $pieces_count;	$i++	)
								{
												$a_sql_query = trim( $pieces[$i] );
												if ( !empty( $a_sql_query ) && $a_sql_query[0] != "#" )
												{
																$msql->query( $a_sql_query );
												}
								}
				}
				$FromPath = "../update/".$release;
				$ToPath = "../";
				cpfolder( $FromPath, $ToPath );
				$msql->query( "update {P}_base_config set `value`='{$user}' where `variable`='phpwebUser'" );
				echo "OK";
				exit( );
				break;
}
?>
